//
//  BIDMeetingSignViewController.m
//  JuanChengOA
//
//  Created by mal on 2017/7/28.
//  Copyright © 2017年 mal. All rights reserved.
//

#import "BIDMeetingSignViewController.h"
#import "BIDSignMeetingDetailViewController.h"
#import "SGQRCode.h"

@interface BIDMeetingSignViewController ()<SGQRCodeScanManagerDelegate>

@property (strong, nonatomic) SGQRCodeScanningView *scanningView;

@end

@implementation BIDMeetingSignViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"会议签到";
    [self.view addSubview:self.scanningView];
    [self setupQRCodeScanning];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.scanningView addTimer];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.scanningView removeTimer];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (SGQRCodeScanningView *)scanningView
{
    if (!_scanningView)
    {
        _scanningView = [SGQRCodeScanningView scanningViewWithFrame:self.view.bounds layer:self.view.layer];
    }
    return _scanningView;
}

- (void)setupQRCodeScanning
{
    SGQRCodeScanManager *manager = [SGQRCodeScanManager sharedManager];
    NSArray *arr = @[AVMetadataObjectTypeQRCode, AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeCode128Code];
    // AVCaptureSessionPreset1920x1080 推荐使用，对于小型的二维码读取率较高
    [manager SG_setupSessionPreset:AVCaptureSessionPreset1920x1080 metadataObjectTypes:arr currentController:self];
    manager.delegate = self;
}

- (void)removeScanningView
{
    [self.scanningView removeTimer];
    [self.scanningView removeFromSuperview];
    self.scanningView = nil;
}

#pragma mark - SGQRCodeScanManagerDelegate
- (void)QRCodeScanManager:(SGQRCodeScanManager *)scanManager didOutputMetadataObjects:(NSArray *)metadataObjects
{
    NSLog(@"metadataObjects - - %@", metadataObjects);
    if (metadataObjects != nil && metadataObjects.count > 0)
    {
        AVMetadataMachineReadableCodeObject *obj = metadataObjects[0];
        NSLog(@"%@", [obj stringValue]);
        NSString *strRes = [obj stringValue];
        NSData *data = [[NSData alloc] initWithData:[strRes dataUsingEncoding:NSUTF8StringEncoding]];
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        if([dic[@"flag"] isEqualToString:@"jcxgovQR"])
        {
            [scanManager SG_palySoundName:@"SGQRCode.bundle/sound.caf"];
            [scanManager SG_stopRunning];
            [scanManager SG_videoPreviewLayerRemoveFromSuperlayer];
            BIDSignMeetingDetailViewController *vc = [[BIDSignMeetingDetailViewController alloc] initWithNibName:@"BIDSignMeetingDetailViewController" bundle:nil];
            vc.meetingID = dic[@"noticeId"];
            NSMutableArray *arr = [[NSMutableArray alloc] initWithArray:self.navigationController.viewControllers];
            [arr removeLastObject];
            [arr addObject:vc];
            [self.navigationController setViewControllers:arr animated:YES];
            [self.navigationController pushViewController:vc animated:YES];
        }
        else
        {
        }
    }
    else
    {
        NSLog(@"暂未识别出扫描的二维码");
    }
}

@end
